//
//  Ext+UIButton.swift
//  weibo_zha
//
//  Created by _zha on 16/10/24.
//  Copyright © 2016年 渣. All rights reserved.
//

import UIKit

extension UIButton {
    ///图片＋背景图片＋点击事件
    convenience init(setImgName: String, backgroundImgName: String, target: Any?, action:Selector) {
        self.init()
        //添加点击事件
        self.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        //设置图片
        self.setImage(UIImage(named:setImgName), for: UIControlState.normal)
        self.setImage(UIImage(named:"\(setImgName)_highlighted"), for: UIControlState.highlighted)
        //设置背景图片
        self.setBackgroundImage(UIImage(named:backgroundImgName), for: UIControlState.normal)
        self.setBackgroundImage(UIImage(named:"\(backgroundImgName)_highlighted"), for: UIControlState.highlighted)
        self.sizeToFit()
    }
    
    
    ///文字＋高亮图片＋点击事件
    convenience init(setHighlightedImgName: String? = nil, title: String? = nil, target: Any?, action: Selector) {
        self.init()
        // 添加点击事件
        self.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        if let img = setHighlightedImgName {
            // 设置 image
            self.setImage(UIImage(named: img), for: UIControlState.normal)
            self.setImage(UIImage(named: "\(img)_highlighted"), for: UIControlState.highlighted)
        }
        
        if let tit = title {
            self.setTitle(tit, for: UIControlState.normal)
            self.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
            self.setTitleColor(AOHomeColor, for: UIControlState.highlighted)
            titleLabel?.font = UIFont.systemFont(ofSize: 14)
        }
        self.sizeToFit()
    }
    
    
    ///文字（大小，颜色）＋背景图片＋点击事件
    convenience init(setBackgroundImgName: String, title:  String, fontSize: CGFloat, titleColor: UIColor,  target: Any?, action: Selector) {
        self.init()
        // 添加点击事件
        self.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        self.setBackgroundImage(UIImage(named: setBackgroundImgName), for: UIControlState.normal)
        self.setTitle(title, for: UIControlState.normal)
        self.setTitleColor(titleColor, for: UIControlState.normal)
        titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        self.sizeToFit()
    }
    
}
